package com.knowvoyage.knowvoyageserver.service.Impl;

import com.knowvoyage.knowvoyageserver.mapper.DialogMapper;
import com.knowvoyage.knowvoyageserver.pojo.entity.Dialog;
import com.knowvoyage.knowvoyageserver.pojo.vo.Result;
import com.knowvoyage.knowvoyageserver.service.DialogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
@RequiredArgsConstructor
public class DialogServiceImpl implements DialogService {
    private final DialogMapper dialogMapper;

    @Override
    public Result<Boolean> insert(Dialog dialog) {
        Long dialogId = dialog.getDialogId();
        if (dialogId == null) {
            return Result.error(400,"对话ID为空");
        }
        if(dialogMapper.selectById(dialogId) != null) {
            return Result.error(400,"对话ID已存在");
        }
        dialogMapper.insert(dialog);
        return Result.success(true);
    }

    @Override
    public Result<Boolean> delete(Long dialogId) {
        if (dialogId == null) {
            return Result.error(400,"对话ID为空");
        }
        if(dialogMapper.selectById(dialogId) == null) {
            return Result.error(400,"对话不存在");
        }
        dialogMapper.deleteById(dialogId);
        return Result.success(true);
    }

    @Override
    public Result<Boolean> update(Dialog dialog) {
        Long dialogId = dialog.getDialogId();
        if (dialogId == null) {
            return Result.error(400,"对话ID为空");
        }
        if(dialogMapper.selectById(dialogId) == null) {
            return Result.error(400,"对话不存在");
        }
        dialogMapper.update(dialog);
        return Result.success(true);
    }

    @Override
    public Result<Dialog> selectById(Long dialogId) {
        if (dialogId == null) {
            return Result.error(400,"对话ID为空");
        }
        return Result.success(dialogMapper.selectById(dialogId));
    }

    @Override
    public Result<List<Dialog>> selectByUserId(Long userId) {
        if (userId == null) {
            return Result.error(400,"用户ID为空");
        }
        return Result.success(dialogMapper.selectByUserId(userId));
    }

    @Override
    public Result<List<Dialog>> selectAll() {
        return Result.success(dialogMapper.selectAll());
    }
}
